package com.zmh.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.zmh.model.ResultMsg;
import com.zmh.model.book.BookInfo;
import com.zmh.model.book.WebsiteInfo;
import com.zmh.model.wechat.httpClient.MessageInfo;
import com.zmh.service.book.BookService;
import com.zmh.service.book.BookUpdateService;
import com.zmh.service.msg.MsgService;
import com.zmh.service.wechat.WeChatService;

/** 
 * 消息管理
 * @author zhoumh
 * @version 创建时间：2018年06月23日 下午4:29:19 
 * @Description 
*/
@RestController
@RequestMapping("/msgManager")
public class MsgManagerController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private MsgService msgService;
	
	/**
	 * 分页查询消息列表
	 * @param page 页码
	 * @param limit 条数
	 * @return: ResultMsg
	 * @author: zhoumh
	 * @version 创建时间：2018年5月13日 下午6:04:53
	 */
	@RequestMapping(value = "/getMsgList")
	public ResultMsg getMsgList(
			@RequestParam(value="page", required=true) String page,
			@RequestParam(value="limit", required=true) String limit) {
		//获取分页
		List<MessageInfo> msgInfos = msgService.getMsgList(page, limit);
		return new ResultMsg("0", "正常", 10, msgInfos);
	}
	/**
	 * 根据ID查询消息
	 * @param id 书籍ID
	 * @return ResultMsg
	 * @author zhoumh
	 * @version 创建时间：2018年5月13日 下午6:12:35
	 */
	@RequestMapping(value = "/getMsgById")
	public ResultMsg getBookById(@RequestParam(value="id") String id){
		MessageInfo msg = msgService.getMsgById(id);
		return new ResultMsg("0","正常",1,new ArrayList<>(Arrays.asList(msg)));
	}
	
	/**
	 * 增加消息
	 * @param BookInfo 书籍信息
	 * @return int 受影响的行数
	 * @author zhoumh
	 * @version 创建时间：2018年5月13日 下午6:12:35
	 * @Description:
	 */
	@RequestMapping(value = "/addMsg",method = RequestMethod.POST,consumes = "application/json; charset=utf-8")
	public ResultMsg addBook(@RequestBody MessageInfo msg){
		System.out.println(JSONObject.toJSONString(msg));
		int result = msgService.addMsg(msg);
		return new ResultMsg("0","增加成功");
	}
	
	/**
	 * 修改消息
	 * @author zhoumh
	 * @version 创建时间：2018年5月13日 下午6:18:54
	 */
	@RequestMapping(value = "/updMsgById",method = RequestMethod.POST,consumes = "application/json; charset=utf-8")
	public ResultMsg updWebsiteById(@RequestBody MessageInfo msg){
		System.out.println(JSONObject.toJSONString(msg));
		int result = msgService.updMsgById(msg);
		return new ResultMsg("0","更新成功");
	}
	/**
	 * 根据ID删除消息
	 * @param id 网站ID
	 * @author: zhoumh
	 * @version: 创建时间：2018年5月6日 下午7:26:00
	 * @Description:
	 */
	@RequestMapping(value = "/delMsgById")
	public ResultMsg delBookById(@RequestParam(value="id") String id) {
		System.out.println("要删除的id: "+id);
		int result = msgService.delMsgById(id);
		return new ResultMsg("0", "删除成功");
	}
	

}
